package mysql

import (
	"doctor-medicine-system/doctorrpc/doctorrpc"
	"gorm.io/gorm"
)

type Good struct {
	gorm.Model
	GoodName    string `gorm:"type:varchar(20);not null"`
	GoodAddress string `gorm:"type:varchar(300);not null"`
	GoodNum     int64  `gorm:"type:int(11);not null"`
	DoctorId    int64  `gorm:"type:int(11);not null"`
}

func (g *Good) GoodAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

/*
SELECT
  goods.id,
  goods.doctor_id as doctor_id,
  goods.good_name,
  goods.good_address,
  goods.good_num
FROM
  goods
  LEFT JOIN doctors ON goods.doctor_id = doctors.id
*/

func GoodList(db *gorm.DB, id int64) []*doctorrpc.GoodList {
	var goodlist []*doctorrpc.GoodList

	db.Debug().Model(&Good{}).
		Select(
			"goods.id as good_id",
			"goods.doctor_id as doctor_id",
			"goods.good_name",
			"goods.good_address",
			"goods.good_num").
		Joins("LEFT JOIN doctors ON goods.doctor_id = doctors.id").
		Where("goods.doctor_id=?", id).
		Find(&goodlist)

	return goodlist

}
